<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>节流</title>
</head>
<body>
  <div id="app" draggable="true"></div>
  <script>
    const box = document.getElementById('app');
    // 节流: 一段时间内，只执行一次某个操作，过了这段时间，继续执行新的操作
    function throttle(fn, delay) {
      let timer = null
      return function () {
        if (timer) {
          return
        }
        timer = setTimeout(() => {
          fn()
          timer = null
        }, delay)
      }
    }
    box.addEventListener('drag', throttle(function(e) {
      console.log('test')
    }, 200))
  </script>
</body>
<style>
  #app {
    width: 200px;
    height: 200px;
    background-color: red;
  }
</style>
</html>